package com.wanlian.router.ui.manage;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.smart.android.ui.BaseFragment;
import com.smart.android.widget.EditTextWithClear;
import com.wanlian.router.R;
import com.wanlian.router.bean.RouterInfo;
import com.wanlian.router.bean.WifiSetting;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/9/8.
 * Email: fvaryu@163.com
 */
public class PPPoESettingFragment extends BaseFragment implements TextWatcher {

    @BindView(R.id.et_account)
    EditTextWithClear et_account;
    @BindView(R.id.et_pwd)
    EditTextWithClear et_pwd;

    @BindView(R.id.tv_next)
    TextView next;
    @BindView(R.id.image_check)
    ImageView mCheckEye;
    private boolean showPwd;


    public static PPPoESettingFragment newInstance() {
        Bundle args = new Bundle();
        PPPoESettingFragment fragment = new PPPoESettingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int layout() {
        return R.layout.fragment_pppoe;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        et_account.addTextChangedListener(this);
        et_pwd.addTextChangedListener(this);
    }

    @Override
    protected void initData(View view) {
        super.initData(view);

        NetWorkingClient.getInstance().queryCurrentRouterInfo(getActivity(), new INetCallBack<RouterInfo>() {
            @Override
            public void onFinish(ResponseData response, @Nullable RouterInfo info) {
                if (response.isSuccess()) {
                    if (info != null) {
                        if (info.getDeviceConfig() == null) {
                            return;
                        }
                        WifiSetting wifiSetting = info.getDeviceConfig();
                        et_account.setText(wifiSetting.getNetworkAccount());
                        et_pwd.setText(wifiSetting.getNetworkPassWord());


                    }
                }
            }
        });
    }

    @OnClick(R.id.image_check)
    void check() {
        showPwd = !showPwd;
        if (showPwd) {
            mCheckEye.setImageResource(R.drawable.eye_open_gray);
            et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            mCheckEye.setImageResource(R.drawable.eye_close_gray);
            et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }


    }

    @OnClick(R.id.tv_next)
    void next() {


        NetWorkingClient.getInstance().connectCall(getActivity(), et_account.getText().toString(), et_pwd.getText().toString(), new INetCallBack() {
            @Override
            public void onFinish(ResponseData response, @Nullable Object o) {
                if (response.isSuccess()) {
                    showToast("连接成功");
                } else {
                    showToast(response.getMessage());
                }
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

        next.setEnabled(et_account.getText() != null &&
                !TextUtils.isEmpty(et_account.getText().toString()) &&
                et_pwd.getText() != null &&
                !TextUtils.isEmpty(et_pwd.getText().toString()));
    }
}
